package tto.gio.next;


import java.util.List;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;




public class HiddenView extends RelativeLayout {
	public HiddenView(HiddenService service){
		super(service.getApplicationContext());
		mContext = service.getApplicationContext();
		
		load();
	}
	@TargetApi(16)
	protected void setupLayoutParams() {
		// Override this to modify the initial LayoutParams. Be sure to call
		// super.setupLayoutParams() first.

		layoutParams = new WindowManager.LayoutParams(//WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT,
	            WindowManager.LayoutParams.WRAP_CONTENT,
	            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
	            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
	            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
	            PixelFormat.TRANSLUCENT);

		layoutParams.gravity = Gravity.BOTTOM;
				
	}
	protected void inflateView() {
		// Inflates the layout resource, sets up the LayoutParams and adds the
		// View to the WindowManager service.

		//LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		//inflater.inflate(R.layout.hidden, this);
		
	}
	public boolean isVisible() {
		// Override this method to control when the Overlay is visible without
		// destroying it.
		return true;
	}
	protected void reloadLayout() {
		unload();
		load();
	}
	public void refreshLayout() {
		// Call this to force the updating of the view's layout.

		removeAllViews();
		inflateView();
		refresh();
	}
	protected void addView() {
		setupLayoutParams();
		((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).addView(this, layoutParams);
	}
	protected void load() {
		inflateView();
		addView();
		refresh();
	}
	
	
	
	
	protected void unload() {
		((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).removeView(this);

		removeAllViews();
	}
	public void destory() {
		((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).removeView(this);
		removeAllViews();
	}
	
	public void makeActive() {
		// Call this make the overlay active and start receiving all touch events
		
		layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

		((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).updateViewLayout(this, layoutParams);
	}
	
	public void makeInactive() {
		layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

		WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);

		wm.updateViewLayout(this, layoutParams);
	}
	
	protected void refreshViews() {
		// Override this method to refresh the views inside of the Overlay. Only
		// called when Overlay is visible.
	}
	public void refresh() {
		// Call this to update the contents of the Overlay.

		if (!isVisible()) {
			setVisibility(View.GONE);
		} else {
			setVisibility(View.VISIBLE);

			refreshViews();
		}
	}



	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		boolean res  = super.onTouchEvent(event);
		if (event.getActionMasked() == MotionEvent.ACTION_UP) {

			GetTopPackage();
		}

		return res;

	}
	public void GetTopPackage() {
		ActivityManager activityManager = (ActivityManager) mContext
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> info;
		info = activityManager.getRunningTasks(1);
		//for (Iterator iterator = info.iterator(); iterator.hasNext();) {
		//	RunningTaskInfo runningTaskInfo = (RunningTaskInfo) iterator.next();
		//	Log.d(tag,runningTaskInfo.topActivity.getClassName().toString());
		//	Log.d(tag,runningTaskInfo.topActivity.getClassName().toString());
		//}
		RunningTaskInfo topTask = (RunningTaskInfo)info.get(0);
		Log.d(tag,topTask.topActivity.getPackageName().toString());
		Log.d(tag,topTask.topActivity.getClassName().toString());
		
		
	}
	private String tag="tto.gio.next";
	private Context mContext;
	protected WindowManager.LayoutParams layoutParams;
}
